package my_web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Proxy.DynamicProxy;
import ReqBean.RequestBean;
import jsonDate.JsonDateValueProcessor;
import my_web.Interface.MissionInterface;
import my_web.bean.Emp;
import my_web.model.EmpDto;
import my_web.model.MissionDto;
import my_web.page.Page;
import my_web.service.MissionService;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
@WebServlet({"/MissionToList.do","/MissionToAdd.do","/MissionDoAdd.do","/MissionDoDel.do","/MissionToUpd.do","/MissionDoUpd.do","/MissionToDeliver.do","/MissionToEmp.do","/MissionDoDeliver.do","/toInfoList.do"})
public class MissionServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		String path=req.getServletPath();
		req.setCharacterEncoding("utf-8");
		res.setContentType("text/html;charset=utf-8");
		HttpSession session=req.getSession();
		MissionInterface service=null;
		try {
			service=DynamicProxy.creatServiceProxy(MissionInterface.class, MissionService.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if("/MissionToList.do".equals(path)){//任务列表
			try {
				res.setContentType("application/json;charset=UTF-8");//设置媒体格式
				Emp emp=(Emp)session.getAttribute("Emp");//获取当前登陆用户
				MissionDto dto=RequestBean.getBeanFromRequest(MissionDto.class, req);//获取请求参数dto
				Page pg=(Page)service.DynamicQueryAllMissions(dto, emp);//显示所有符合条件的任务
				/*
				 * javaBean对象转换为json对象
				 * 说明：数据库查询出来的结果 转成一个json格式的数据 
				 * 这个json的数据格式要带两个属性：
				 * 1. 一个是total 总记录数 
				 * 2.一个是rows 就是明细
				 */
				JsonConfig jsonConfig=new JsonConfig();
				jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());//时间处理
				//String json=JSONObject.fromObject(pg.getEasyPage(),jsonConfig).toString();
				String json=JSONObject.fromObject(pg.getEasyPage2(),jsonConfig).toString();
				PrintWriter out = res.getWriter();
				out.print(json);
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}	
		}else if("/MissionToAdd.do".equals(path)){//去增加
			PrintWriter out = res.getWriter();
			out.print("" + "\"" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "\"" + "");
			out.flush();
			out.close();
		}else if("/MissionDoAdd.do".equals(path)){//做增加
			PrintWriter out = res.getWriter();
			try {
				Emp emp=(Emp)session.getAttribute("Emp");
				MissionDto dto=RequestBean.getBeanFromRequest(MissionDto.class, req);
				dto.setM_start_date(new Date());
				dto.setE_id(emp.getE_id());
				dto.setM_state("未发布");
				service.addMission(dto);
				out.print("{\"success\":true,\"msg\":\"添加成功\"}");
			} catch (Exception e) {
				e.printStackTrace();
				out.print("{\"success\":false,\"msg\":\"添加失败\"}");
			} 
			out.flush();
			out.close();
		}else if("/MissionDoDel.do".equals(path)){//做删除
			PrintWriter out = res.getWriter();
			try {
				res.setContentType("application/json;charset=UTF-8");
				String m_id=req.getParameter("m_id");
				service.deleteMissionById(m_id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			} catch (Exception e) {
				e.printStackTrace();
				out.print("{\"success\":false,\"msg\":\"删除失败\"}");
			}
			out.flush();
			out.close();
		}else if("/MissionToUpd.do".equals(path)){//去修改
			PrintWriter out = res.getWriter();
			try {
				res.setContentType("application/json;charset=UTF-8");
				String m_id=req.getParameter("m_id");
				MissionDto dto=service.getMissionById(m_id);
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
				String json = JSONObject.fromObject(dto, jsonConfig).toString();
				out.print(json);
			} catch (Exception e) {
				e.printStackTrace();
			} 
			out.flush();
			out.close();
		}else if("/MissionDoUpd.do".equals(path)){//做修改
			PrintWriter out = res.getWriter();
			try {
				res.setContentType("application/json;charset=UTF-8");
				MissionDto dto=RequestBean.getBeanFromRequest(MissionDto.class, req);
				service.updateMission(dto);
				out.print("{\"success\":true,\"msg\":\"修改成功\"}");
			} catch (Exception e) {
				e.printStackTrace();
				out.print("{\"success\":false,\"msg\":\"修改失败\"}");
			} 
			out.flush();
			out.close();
		}else if("/MissionToDeliver.do".equals(path)){
			String m_id = req.getParameter("m_id");//任务Id
			System.out.println("====="+m_id);
			Emp emp = (Emp) session.getAttribute("Emp");//当前登录用户-任务发起者
			List<Emp> list1=service.getEmp(emp);//查询本部门的所有员工
			MissionDto dto =service.getMission(m_id);//获取该假条信息
			Map map = new HashMap();
			map.put("dto", dto);
			map.put("list", list1);
			res.setContentType("application/json;charset=UTF-8");
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
			String json = JSONObject.fromObject(map, jsonConfig).toString();//把含有假条信息和本部门员工信息的map响应回去
			PrintWriter out = res.getWriter();
			out.print(json);
			out.flush();
			out.close();
		}
		else if("/MissionToEmp.do".equals(path)){
			 Emp emp = (Emp)req.getSession().getAttribute("Emp");
			try {
				EmpDto	dto = RequestBean.getBeanFromRequest(EmpDto.class, req);
				Page pg = service.getAllMissionEmp(dto,emp);
				res.setContentType("application/json;charset=UTF-8");
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
				String json = JSONObject.fromObject(pg.getEasyPage(), jsonConfig).toString();
				PrintWriter out = res.getWriter();
				out.print(json);
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("/MissionDoDeliver.do".equals(path)){//做派发
			res.setContentType("application/json;charset=UTF-8");
			PrintWriter out  = res.getWriter();
			try {
				service.DoDeliver(req.getParameter("e_id"),req.getParameter("m_id"));
				out.print("{\"msg\":\"派发成功!\"}");
			} catch (Exception e) {
				out.print("{\"msg\":\"派发失败!\"}");
			}
		}else if("/toInfoList.do".equals(path)){
			String m_id=req.getParameter("m_id");
			req.setAttribute("mission",service.getMissionInfoById(m_id));
			req.getRequestDispatcher("missionInfo.jsp").forward(req, res);
		}
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		this.doGet(req, res);
	}
}
